<template>
    <div>
        <Button icon="ios-download-outline" @click="DownloadTemplate" type="dashed">下载模板</Button><br /><br />
        <Upload :before-upload="handleUpload" :data="file" action="/api/purchaseorder/ImportExcel" accept=".xls, .xlsx"
            :max-size="size">
            <Button icon="ios-cloud-upload-outline">请选择药品入库模板</Button>
        </Upload>
        <div v-if="file !== null">上传文件: {{ file.name }} <Button type="text" @click="upload" :loading="loadingStatus">{{
            loadingStatus ? 'Uploading' : '点击上传' }}</Button></div>
    </div>
</template>
<script>
import { ImportExcel, DownloadTemplate, sysFileInfoDownload } from '@/api/purchaseorder'
export default
    {
        name: 'purchaseImport',
        data() {
            return {
                file: null,
                loadingStatus: false,
                importData: [],
                actionUrl: 'api/purchaseorder/ImportExcel',
                size: 5120

            }
        },
        computed: {},
        methods: {
            handleUpload(file) {
                this.file = file;
                return false;
            },
            upload() { // 上传文件
                let v = this
                const formData = new FormData()
                formData.append('file', v.file)
                ImportExcel(formData).then((res) => {
                    debugger
                    if (res.status == 200) {
                        if (res.data.Succeeded) {
                            v.loadingStatus = false;
                            v.importData = res.data.Data;
                            v.choseData()
                        }
                        else {
                            v.$Message.error({ title: '导入错误：', content: res.data.Errors })
                        }
                    }
                }).catch(err => {
                    v.$Message.error({ title: '导入错误：', content: err })
                })
            },
            choseData() {
                this.$emit('on-success', this.importData)
            },
            DownloadTemplate() {
                let v = this
                sysFileInfoDownload().then((res) => {
                    debugger;
                    this.downloadfile(res)
                }).catch((err) => {
                    v.$Message.error({ title: '下载错误', content: err })
                })
            },
            downloadfile(res) {
                debugger;
                var blob = new Blob([res.data], { type: 'application/vnd.ms-excel;charset=utf-8' })
                var contentDisposition = res.headers['content-disposition']
                var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
                var filename = "药品入库导入模板.xlsx"
                if (contentDisposition != undefined) {
                    var result = patt.exec(contentDisposition)
                    filename = result[1]
                }
                var downloadElement = document.createElement('a')
                var href = window.URL.createObjectURL(blob) // 创建下载的链接
                var reg = /^["](.*)["]$/g
                downloadElement.style.display = 'none'
                downloadElement.href = href
                downloadElement.download = decodeURI(filename.replace(reg, '$1')) // 下载后文件名
                document.body.appendChild(downloadElement)
                downloadElement.click() // 点击下载
                document.body.removeChild(downloadElement) // 下载完成移除元素
                window.URL.revokeObjectURL(href)
            },

        },
        mounted() {
            this.importData = [];
        }
    }
</script>
